import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询角色信息
 */
export async function pageMesRoles(params) {
  const res = await request.get('/mes/mesRole/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部角色信息
 */
export async function listMesRoles(params) {
  const res = await request.get('/mes/mesRole/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询角色信息
 */
export async function getMesRole(id) {
  const res = await request.get('/mes/mesRole/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加角色信息
 */
export async function addMesRole(data) {
  const res = await request.post('/mes/mesRole', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改角色信息
 */
export async function updateMesRole(data) {
  const res = await request.put('/mes/mesRole', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除角色信息
 */
export async function removeMesRole(id) {
  const res = await request.delete('/mes/mesRole/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除角色信息
 */
export async function removeMesRoles(ids) {
  const res = await request.delete('/mes/mesRole/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出角色信息
 */
export async function exportMesRoles(params) {
  const res = await request({
    url: '/mes/mesRole/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesRole_${Date.now()}.xlsx`);
}
